#!/bin/bash

set -o pipefail;

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "

if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx 'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INS_FILE} ]
  then
    rm -f ${OUT_INS_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INS_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME batch_recv_date_in_MMDDYY_fotmat";
}

if [ $# -lt 1 ]
then
  usage;
  exit 10;
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${1}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"

# set file locations
if [ ! -d ${BATCH_CTX_DIR} ]
then
  mkdir ${BATCH_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : unable to create batch dir : ${BATCH_CTX_DIR}"
    exit 10;
  fi
fi

# rcpt for encr
ERCPT_MDLX="-r mkey_mdlx"

#GPG STATUS FILE
EDER_DSTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_trans.d
EDER_ESTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_trans.e
ENCR="${ENCR} -s ${EDER_ESTATUS_FILE}"
DECR="${DECR} -s ${EDER_DSTATUS_FILE}"


# TRANS BAK
INS_BAK_FILE=${ARCH_CTX_BAK_DIR}/${BATCH_RECV_DATE}_trans_t.gpg

# BATCH STATUS
BATCH_STATUS_BAK_FILE=${ARCH_CTX_BAK_DIR}/${BATCH_RECV_DATE}_batch_status_trans_t.gpg



TASK_ID=0
tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : BACKUP PART-1 START"




TASK_ID=`expr $TASK_ID + 1`
tmsg="backup trans";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_BAK_DIR}/copy_table_trans.sql \
  | ${ENCR} ${ERCPT_MDLX} -f - > ${INS_BAK_FILE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))




TASK_ID=`expr $TASK_ID + 1`
tmsg="backup batch_status_trans";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_BAK_DIR}/copy_table_batch_status_trans.sql \
  | ${ENCR} ${ERCPT_MDLX} -f - > ${BATCH_STATUS_BAK_FILE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


tstamp="$(mltstamp ${tstart})"
mllogerr "[${LOG_CTX}] : END : [${tstamp}] : BACKUP PART-1 DONE"

exit 0;
